package com.gpframework.module.controller.module;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.module.domain.GpAccountManagement;
import com.gpframework.module.module.domain.GpSupplierManagement;
import com.gpframework.module.module.domain.req.GpSupplierManagementAdd;
import com.gpframework.module.module.domain.req.GpSupplierManagementReq;
import com.gpframework.module.module.domain.req.GpSupplierManagementUpdate;
import com.gpframework.module.module.domain.vo.GpSupplierManagementExcel;
import com.gpframework.module.module.domain.vo.GpSupplierManagementVo;
import com.gpframework.module.module.service.IGpAccountManagementService;
import com.gpframework.module.module.service.IGpSupplierManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GpSupplierManagementController
 * @Author gpframework
 * @Date 2024-01-08
 * @Version 1.0.0
 * @Description 供应商管理Controller
 */
@Validated
@Api(tags = "供应商管理")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/module/supplier" )
public class GpSupplierManagementController extends BaseController {

    private final IGpSupplierManagementService iGpSupplierManagementService;
    private final IGpAccountManagementService iGpAccountManagementService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-08
  * @Param gpSupplierManagement
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('module:supplier:list')")
    @GetMapping("/list")
    public TableDataInfo<GpSupplierManagementVo> list(GpSupplierManagementReq gpSupplierManagementReq)
    {
        startPage();
        QueryWrapper<GpSupplierManagement> lqw = new QueryWrapper<GpSupplierManagement>();
        gpSupplierManagementReq.generatorQuery(lqw,true);
        List<GpSupplierManagementVo> list = iGpSupplierManagementService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpSupplierManagement
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('module:supplier:add')" )
    @Log(title = "添加供应商管理" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpSupplierManagementAdd gpSupplierManagementAdd) {
        GpSupplierManagement gpSupplierManagement = Convert.convert(new TypeReference<GpSupplierManagement>() {}, gpSupplierManagementAdd);
        return toAjax(iGpSupplierManagementService.save(gpSupplierManagement) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpSupplierManagement
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('module:supplier:edit')" )
    @Log(title = "修改供应商管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpSupplierManagementUpdate gpSupplierManagementUpdate) {
        GpSupplierManagement gpSupplierManagement = Convert.convert(new TypeReference<GpSupplierManagement>() {}, gpSupplierManagementUpdate);
        return toAjax(iGpSupplierManagementService.updateById(gpSupplierManagement) ? 1 : 0);
    }


    /**
     * @Description 导出供应商管理模板
     * @Author gpframework
     * @Date 2023-05-24
     * @Param gpCustomer
     * @Return TableDataInfo
     */
    @ApiOperation("导出供应商管理模板")
    @Log(title = "导出供应商管理模板" , businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate" )
    public AjaxResult exportTemplate() {
        ExcelUtil<GpSupplierManagementExcel> util = new ExcelUtil<>(GpSupplierManagementExcel.class);
        return util.exportExcel(GpSupplierManagementExcel.list(), "模板");
    }

    /**
     * @Description 导入供应商管理
     * @Author gpframework
     * @Date 2023-10-27
     * @Param (GpMember
     * @Return TableDataInfo
     */
    @Log(title = "导入供应商管理" , businessType = BusinessType.IMPORT)
    @PostMapping("/import" )
    @ApiOperation("导入供应商管理")
    public AjaxResult entityImport(MultipartFile file, Long brandId, Long storeId) throws Exception {
        ExcelUtil<GpSupplierManagementExcel> util = new ExcelUtil<GpSupplierManagementExcel>(GpSupplierManagementExcel. class);
        List<GpSupplierManagementExcel> excelList = util.importExcel(file.getInputStream());

        return AjaxResult.builder(() -> {
            return this.iGpSupplierManagementService.gpSupplierManagementImport(excelList, brandId, storeId);
        });
    }

    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpSupplierManagement
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('module:supplier:export')" )
    @Log(title = "导出供应商管理" , businessType = BusinessType.EXPORT)
    @ApiOperation("导出供应商管理")
    @GetMapping("/export" )
    public AjaxResult export(GpSupplierManagementReq gpSupplierManagementReq) {
        QueryWrapper<GpSupplierManagement> lqw = new QueryWrapper<GpSupplierManagement>();
        gpSupplierManagementReq.generatorQuery(lqw,true);
        List<GpSupplierManagementVo> list = iGpSupplierManagementService.listVo(lqw);
        ExcelUtil<GpSupplierManagementVo> util = new ExcelUtil<GpSupplierManagementVo>(GpSupplierManagementVo. class);
        return util.exportExcel(list, "supplier" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpSupplierManagement
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('module:supplier:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpSupplierManagementVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpSupplierManagementService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpSupplierManagement
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('module:supplier:remove')" )
    @Log(title = "删除供应商管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpSupplierManagementService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 绑定账户
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCustomerManagement
     * @Return TableDataInfo
     */
    @PreAuthorize("@ss.hasPermi('module:customer:bindAccount')" )
    @PutMapping(value = "/bindAccount" )
    public AjaxResult<GpSupplierManagementVo> bindAccount(@Valid @RequestBody GpSupplierManagementUpdate gpSupplierManagementUpdate) {
        if (gpSupplierManagementUpdate.getAccountId()==null && gpSupplierManagementUpdate.getId()==null){
            return AjaxResult.parameterError();
        }
        GpAccountManagement gpAccountManagement = iGpAccountManagementService.getById(gpSupplierManagementUpdate.getAccountId());
        if (gpAccountManagement==null){
            return AjaxResult.error("绑定的账户不存在");
        }
        GpSupplierManagement supplierManagement = iGpSupplierManagementService.getById(gpSupplierManagementUpdate.getId());

        supplierManagement.setAccountId(supplierManagement.getId());
        supplierManagement.setBindingFlag("1");

        iGpSupplierManagementService.updateById(supplierManagement);
        return AjaxResult.success();
    }


    /**
     * @Description 解绑绑定账户
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCustomerManagement
     * @Return TableDataInfo
     */
    @PreAuthorize("@ss.hasPermi('module:customer:unbindAccount')" )
    @PutMapping(value = "/unbindAccount" )
    public AjaxResult<GpSupplierManagementVo> unbindAccount(@Valid @RequestBody GpSupplierManagementUpdate gpSupplierManagementUpdate) {
        if (gpSupplierManagementUpdate.getId()==null){
            return AjaxResult.parameterError();
        }
        GpSupplierManagement gpSupplierManagement = iGpSupplierManagementService.getById(gpSupplierManagementUpdate.getId());
        if (gpSupplierManagement!=null){
            iGpSupplierManagementService.update(new UpdateWrapper<GpSupplierManagement>().set("account_id",null));
            iGpSupplierManagementService.update(new UpdateWrapper<GpSupplierManagement>().set("binding_flag",0));

        }

        return AjaxResult.success();
    }
}
